package com.jl.security.mac;

import java.security.GeneralSecurityException;
import java.util.Arrays;

import com.jl.security.MyCipher;

public class ALG_DES_MAC8_ISO9797_M2 implements IMac {

	private byte[] key;
	private byte[] iv = new byte[8];

	@Override
	public void initKey(byte[] key) {
		this.key = key;
	}

	@Override
	public void initIV(byte[] IV) {
		this.iv = IV;
	}

	@Override
	public byte[] doFinal(byte[] data) throws GeneralSecurityException {
		String alg = "";
		if (key.length == 8) {
			alg = "DES/CBC/ISO9797M2";
		} else {
			alg = "DESede/CBC/ISO9797M2";
		}
		byte[] res = MyCipher.encrypt(alg, this.key, data, this.iv);
		return Arrays.copyOfRange(res, res.length - 8, res.length);
	}

}
